package com.ruoyi.business.service;

import com.ruoyi.business.UrlPass;
import com.ruoyi.business.mapper.UrlPassMapper;
import com.ruoyi.common.common.GlobalValue;
import com.ruoyi.common.common.ResultBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author ：jiangguanjun
 * @description：管理员相关
 * @date ：2020/7/16 21:13
 */
@Slf4j
@Component
public class UrlPassService {

    @Autowired
    private UrlPassMapper urlPassMapper;
    @Autowired
    private RedisTemplate redisTemplate;



    public boolean isContainPass(String url, String method) {
        List<String> list = (List<String>) redisTemplate.opsForValue().get(GlobalValue.URL_PASS_KEY);
        if(list==null || list.size()==0){
            return false ;
        }
        String checkValue = url + GlobalValue.URL_PASS_SPLIT+method ;
        log.info("checkValue：：：{}",checkValue);
        return list.contains(checkValue) ;
    }

    public ResultBody add(UrlPass urlPass) {
        int res = urlPassMapper.insert(urlPass);
        if(res>0){
            List<String> list = (List<String>) redisTemplate.opsForValue().get(GlobalValue.URL_PASS_KEY);
            String checkValue = urlPass.getUrl() + GlobalValue.URL_PASS_SPLIT+ urlPass.getReqType().toUpperCase();
            list.add(checkValue) ;
            redisTemplate.opsForValue().set(GlobalValue.URL_PASS_KEY, list);
            return ResultBody.success("添加成功") ;
        }
        return ResultBody.error("添加失败") ;
    }
}
